<?php 
$company_number=$_REQUEST['company_number'];
$date=$_REQUEST['date'];
$sql="SELECT SUM(package_price),company_number,DATE_FORMAT(sale_time,'%Y-%m'),COUNT(*),SUM(bonus) FROM zy_bonus where company_number=".$company_number." AND DATE_FORMAT(sale_time,'%Y-%m')= '".$date."' GROUP BY company_number";
$connection = Yii::app()->db;
$command = $connection->createCommand($sql);
$result = $command->queryAll();
$agentcompany=AgentCompany::model()->find('agent_number='.$company_number);
$agent_type=$agentcompany->agent_type;
$user=User::model()->find('user_id='.$agentcompany->user_id);
if(isset($_POST['submit'])){
	Rebatecommission::model()->createRebatecommission();
	header('Location: admin');
}
?>
<form name="myform" method="post" action="">
<input name="agent_company_id" id="agent_company_id" value="<?php echo $agentcompany->agent_id;?>" type="hidden" />
<input name="user_id" id="user_id" value="<?php echo $agentcompany->user_id?>" type="hidden" />
<input name="date" id="date" value="<?php echo $_REQUEST['date']?>" type="hidden"/>
  <div>
  代理人:<?php  echo $user->user_name;?><br/>
提成:<?php 
for ($i=0;$i<count($result);$i++){
echo $result[$i]['SUM(bonus)']; }?>
  <table class="responsive table table-striped table-bordered">
 	<thead>
 	<tr>
 	<th>套餐总金额</th><th>提成</th>
 	</tr>
 	</thead>
 	<tbody>
 	<tr>
 	<?php 
 	for ($i=0;$i<count($result);$i++){?>
 	<td><?php echo $result[$i]['SUM(package_price)'];?>
 		<input type="hidden" name="package_price" id="package_price" value="<?php echo $result[$i]['SUM(package_price)'];?>" />
 		
 	</td>
 	<td><?php echo $result[$i]['SUM(bonus)'];?><input type="hidden" name="bonus" id="bonus" value="<?php echo $result[$i]['bonus']?>" /></td>
 	<?php } ?>
 	</tr>
 	</tbody>
  </table>
 返点金额:<?php 
 for ($i=0;$i<count($result);$i++){
$number =$result[$i]['COUNT(*)'];
$fandian=Fandian::model()->find("agent_type=".$agent_type."  AND $number between min and max");
echo $result[$i]['SUM(package_price)']*$fandian->rate/100; }?>
 <table class="responsive table table-striped table-bordered">
 	<thead>
 	<tr>
 	<th>返点最小数</th><th>返点最大数</th><th>返点比例</th><th>金额</th><th>返点金额</th>
 	</tr>
 	</thead>
 	<tbody>
 	<tr>
 	<?php 
 	for ($i=0;$i<count($result);$i++){
	?>
	<input type="hidden" name="number" id="number" value="<?php echo $number;?>"/>
	<input type="hidden" name="fandian_id" id="fandian_id" value="<?php echo $fandian->id;?>" />
	<td><?php echo $fandian->min;?><input type="hidden" name="min" id="min" value="<?php echo $fandian->min;?>" /></td>
 	<td><?php echo $fandian->max;?><input type="hidden" name="max" id="max" value="<?php echo $fandian->max;?>" /></td>
 	<td><?php echo $fandian->rate;?><input type="hidden" name="rate" id="rate" value="<?php echo $fandian->rate;?>"/></td>
 	<td><?php echo $result[$i]['SUM(package_price)'];?><input type="hidden" name="sum_package_price" id="sum_package_price" value="<?php echo $result[$i]['SUM(package_price)'];?>"/></td>
 	<td><?php echo $result[$i]['SUM(package_price)']*$fandian->rate/100;?><input type="hidden" name="fandian_money" id="fandian_money" value="<?php echo $result[$i]['SUM(package_price)']*$fandian->rate/100;?>"/></td>
 	<?php } ?>
 	</tr>
 	</tbody>
  </table>
  合计:<?php 
  for ($i=0;$i<count($result);$i++){
 echo  $result[$i]['SUM(bonus)']+$result[$i]['SUM(package_price)']*$fandian->rate/100;
 }
  ?>
  <br/>
  <?php if(yii::app()->user->level==1){?>
          <input type="submit" name="submit" value="<?php echo Yii::t('base','send');?>" id="send"/>
       <?php } ?>
</form>
